package cn.iocoder.yudao.module.erp.controller.admin.business.money.moneyaccount;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.business.money.moneyaccount.vo.MoneyAccountPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.money.moneyaccount.vo.MoneyAccountRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.money.moneyaccount.vo.MoneyAccountSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.business.money.moneyaccount.MoneyAccountDO;
import cn.iocoder.yudao.module.erp.service.business.money.moneyaccount.MoneyAccountService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

@Tag(name = "管理后台 - 资金账户")
@RestController
@RequestMapping("/erp/money-account")
@Validated
public class MoneyAccountController {

    @Resource
    private MoneyAccountService moneyAccountService;

    @PostMapping("/create")
    @Operation(summary = "创建资金账户")
    @PreAuthorize("@ss.hasPermission('erp:money-account:create')")
    public CommonResult<Long> createMoneyAccount(@Valid @RequestBody MoneyAccountSaveReqVO createReqVO) {
        return success(moneyAccountService.createMoneyAccount(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新资金账户")
    @PreAuthorize("@ss.hasPermission('erp:money-account:update')")
    public CommonResult<Boolean> updateMoneyAccount(@Valid @RequestBody MoneyAccountSaveReqVO updateReqVO) {
        moneyAccountService.updateMoneyAccount(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除资金账户")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:money-account:delete')")
    public CommonResult<Boolean> deleteMoneyAccount(@RequestParam("id") Long id) {
        moneyAccountService.deleteMoneyAccount(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得资金账户")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:money-account:query')")
    public CommonResult<MoneyAccountRespVO> getMoneyAccount(@RequestParam("id") Long id) {
        MoneyAccountDO moneyAccount = moneyAccountService.getMoneyAccount(id);
        return success(BeanUtils.toBean(moneyAccount, MoneyAccountRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得资金账户分页")
    @PreAuthorize("@ss.hasPermission('erp:money-account:query')")
    public CommonResult<PageResult<MoneyAccountRespVO>> getMoneyAccountPage(@Valid MoneyAccountPageReqVO pageReqVO) {
        PageResult<MoneyAccountDO> pageResult = moneyAccountService.getMoneyAccountPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MoneyAccountRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出资金账户 Excel")
    @PreAuthorize("@ss.hasPermission('erp:money-account:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMoneyAccountExcel(@Valid MoneyAccountPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MoneyAccountDO> list = moneyAccountService.getMoneyAccountPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "资金账户.xls", "数据", MoneyAccountRespVO.class,
                        BeanUtils.toBean(list, MoneyAccountRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得精简列表", description = "只包含被开启的结算账户，主要用于前端的下拉选项")
    @PreAuthorize("@ss.hasPermission('erp:money-account:query')")
    public CommonResult<List<MoneyAccountRespVO>> getSimpleList() {
        MoneyAccountPageReqVO moneyAccountPageReqVO = new MoneyAccountPageReqVO();
        moneyAccountPageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<MoneyAccountDO> moneyAccountPage = moneyAccountService.getMoneyAccountPage(moneyAccountPageReqVO);
        return success(convertList(moneyAccountPage.getList(), item -> new MoneyAccountRespVO().setId(item.getId())
                .setName(item.getName())));
    }

}